//
//  HXReportEvidenceViewController.m
//  KD
//
//  Created by ifly on 16/7/21.
//  Copyright © 2016年 Whx. All rights reserved.
//

#import "HXReportEvidenceViewController.h"
#import "Imitation_AlertView_TextField.h"
#import "HXUpDataImageModel.h"
#import "UIButton+WebCache.h"
@interface HXReportEvidenceViewController ()<UIImagePickerControllerDelegate,UINavigationControllerDelegate,Imitation_AlertView_TextFielddelegate>

@property (weak, nonatomic) IBOutlet UIImageView *backgroundImageView;//背景照片
@property (weak, nonatomic) IBOutlet UIButton *addPhotoBtn;//添加照片按钮
@property (weak, nonatomic) IBOutlet UILabel *addressLabel;//地址
@property (weak, nonatomic) IBOutlet UILabel *sendTimeLabel;//时间
@property (weak, nonatomic) IBOutlet UILabel *photoDescribeLabel;//描述
@property (nonatomic,strong) HXUpDataImageModel *dict;//上传照片
@property (nonatomic,strong)UILongPressGestureRecognizer * longPress;
@property (nonatomic,strong) UIScrollView *photoScrollView;//照片栏
@property (weak, nonatomic) IBOutlet UIView *informationView;//照片描述视图
@property (nonatomic,strong) NSMutableArray *phothMuArray;//照片
@property (nonatomic,strong) NSMutableArray *photoFilePathArr;//照片网址
@property (nonatomic,strong) UIButton *photoBtn;
@property (weak, nonatomic) IBOutlet UIView *backGroundView;//背景
@property (nonatomic,strong) NSMutableArray *photoDescribeArr;//照片描述数组
@property (weak, nonatomic) IBOutlet UIScrollView *backGroundScrollView;//背景
@property (nonatomic,strong) Imitation_AlertView_TextField * describeTextView;
@property (nonatomic,copy) NSString *address;
@property (nonatomic,copy) NSString *Con_Title;
@property (nonatomic,copy) NSString *con_TaskID;
@property (nonatomic,strong) NSMutableArray *sendMuArray;
@property (nonatomic,copy)NSString *sendTimeStr;
@property (nonatomic,strong) NSMutableArray *getDataArr;
@property (nonatomic,strong) NSMutableArray *timeDateArr;
@property (nonatomic,copy) NSString * updateTime;
@property (nonatomic,assign) BOOL isChange;
@property (nonatomic,strong) UIButton * sendBtn;
@property (nonatomic,copy)NSString *typeNum;
@property (nonatomic,strong) NSMutableArray *judgeArray;
@property (nonatomic,assign) BOOL isJudge;

@property (nonatomic,copy) NSString * Con_SendDate;//点击发布按钮的时间
@property (nonatomic,assign) BOOL   isCon_SendBtn;//是否为点击发布按钮

@end

@implementation HXReportEvidenceViewController


-(instancetype)initWithAddress:(NSString *)Address withCon_Title:(NSString *)Con_Title withCon_TaskID:(NSString *)Con_TaskID {
    if ([super init]) {
        self.address = Address;
        self.Con_Title = Con_Title;
        self.con_TaskID = Con_TaskID;
    }
    return self;
}

-(void)setResultlist:(NSArray<HXJobPunishDetailsListModel *> *)Resultlist {
    
    _Resultlist = Resultlist;

    for (NSInteger i = self.Resultlist.count-1; i>=0; i--) {
        
        HXJobPunishDetailsListModel * model = self.Resultlist[i];
        [self.photoDescribeArr addObject:model.Con_Content];
        [self.timeDateArr addObject:model.Con_Date];
        [self.photoFilePathArr addObject:model.Con_PicUrl];
        NSLog(@"timeDateArr--------------- %@",self.timeDateArr);
        [self.judgeArray addObject:model.Con_PicUrl];
        
        ///点击发布按钮的时间
        self.Con_SendDate = model.Con_SendDate;
    }
    [self getPhotoInScrollView];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    self.backGroundScrollView.contentSize = CGSizeMake(HXSCREEN_WIDTH, self.sendBtn.bottom + 20);
}

//-(void)laodCreateNav{
//    
//    UIButton *backButton=[[UIButton alloc]init];
//    // 设置返回图片
//    [backButton setImage:[UIImage imageNamed:@"nav_back"] forState:UIControlStateNormal];
//    backButton.contentMode = UIViewContentModeLeft;
//    backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
//    // [button sizeToFit];//让按钮大小跟随内容
//    // 让按钮的内容往左边偏移10
//    backButton.contentEdgeInsets = UIEdgeInsetsMake(0, -10, 0, 0);
//    [backButton addTarget:self action:@selector(sendBtnClick:) forControlEvents:UIControlEventTouchUpInside];
//    backButton.tag = 100;
//    backButton.frame=CGRectMake(0, 0,37,90);
//    self.navigationItem.leftBarButtonItem=[[UIBarButtonItem alloc]initWithCustomView:backButton];
//}


- (void)viewDidLoad {
    [super viewDidLoad];
    self.backGroundScrollView.showsVerticalScrollIndicator = NO;
//    self.backgroundImageView.layer.borderWidth = 0.5;
//    self.backgroundImageView.layer.borderColor = [UIColor blackColor].CGColor;
    self.informationView.layer.borderWidth = 0.5;
    self.informationView.layer.borderColor = [UIColor blackColor].CGColor;
    //self.backGroundView.backgroundColor = [UIColor orangeColor];
    [self addPhoto];
    [self creatAlertController];
    if (self.type == SiteReportType) {
        self.title = @"工地报告取证";
        _typeNum = @"3";
    }
    else {
        self.title = @"企业报告取证";
        _typeNum = @"5";
    }
    
    //是否为发布按钮点击的
    self.isCon_SendBtn = NO;
    
    [NetworkAPI getJobPunishListWithTaskId:self.con_TaskID
                                      type:_typeNum
                                   success:^(HXJobPunishDetailsModel *result) {
                                          if ([result.code isEqualToString:@"200"]) {
                                              self.Resultlist = result.resultlist;
                                          }
                                          else {
                                              self.Resultlist = result.resultlist;
                                          }
                                          [self judgeIsChange];
                                      } failure:^(NSError *err) {
                                          
                                      }];
}

/**
 添加照片
 */
-(void)addPhoto {
    
    if (_photoScrollView == nil) {
        _photoScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, self.informationView.y+self.informationView.height+10, HXSCREEN_WIDTH, 100)];
        //_photoScrollView.backgroundColor = [UIColor grayColor];
        _photoScrollView.showsHorizontalScrollIndicator = NO;
        [self.backGroundView addSubview:_photoScrollView];
        //长按手势
        _longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(pictureLongPress:)];
        [_photoScrollView addGestureRecognizer:_longPress];
    }

    _sendBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    _sendBtn.frame = CGRectMake(20, self.photoScrollView.y+self.photoScrollView.height+70, HXSCREEN_WIDTH-40, 37);
    [_sendBtn addTarget:self action:@selector(sendBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [_sendBtn setTitle:@"发布" forState:UIControlStateNormal];
    [_sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    _sendBtn.backgroundColor = [UIColor blueColor];
    [self.backGroundView addSubview:_sendBtn];
}
-(void)sendBtnClick:(UIButton *)button {
//    [self sendPicInformation];
//    NSLog(@"self.sendMuArray------%@",self.timeDateArr);
    if (self.judgeArray.count == 0) {
        
        UIAlertController * alertCon = [UIAlertController alertControllerWithTitle:@"提示" message:@"请完善相关信息后,再次提交" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction * alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        }];
        [alertCon addAction:alertAction];
        [self presentViewController:alertCon animated:YES completion:^{
            
        }];
    }
    else {
     
        if (self.photoFilePathArr.count == 0) {
       
            [NetworkAPI deleteDoorPhotoWithTaskID:_con_TaskID
                                          PicType:_typeNum
                                          Success:^(HXBaseModel *result) {
                                              if ([result.code isEqualToString:@"200"]) {
                                                  //[SVProgressHUD showSuccessWithStatus:@"发送成功"];
                                                  [self tanchuangshitu:@"删除成功,是否退出!"];
                                              }
                                              else {
                                                  [SVProgressHUD showErrorWithStatus:@"网络错误"];
                                              }
                                          } failure:^(NSError *err) {
                                              [SVProgressHUD showErrorWithStatus:@"发送失败"];
                                          }];
            
        }else {
            
            self.isCon_SendBtn = YES;
            
            if (self.Con_SendDate.length <= 0) {
                
                [NetworkAPI getServerDateSuccess:^(HXServerDateModel *result) {
                    
                    if ([result.code integerValue] == 200) {
                        
                        self.Con_SendDate = result.ServerDate;
                        [self loadCreatePicUpload];
                    }
                    
                } failure:^(NSError *err) {
                    
                }];
            }else{
            
                [self loadCreatePicUpload];
            }
            
            
            
            
            
//            NSDictionary *dictModelOne = @{@"dict":self.sendMuArray};
//            NSData * data = [NSJSONSerialization dataWithJSONObject:dictModelOne options:NSJSONWritingPrettyPrinted error:nil];
//            NSString * string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
//            NSLog(@"json------------------------%@",string);
//            [NetworkAPI createAddWithDic:dictModelOne
//                                 success:^(HXSendInformationModel *result) {
//                                     
//                                     if (button.tag == 100) {
//                                         
//                                         [self.navigationController popViewControllerAnimated:YES];
//                                     }
//                                     
//                                     if ([result.code isEqualToString:@"200"]) {
//                                         //[SVProgressHUD showSuccessWithStatus:@"发送成功"];
//                                         if (button.tag == 101) {
//                                             [self tanchuangshitu:@"提交成功,是否退出?"];
//                                         }
//                                     }
//                                     else {
//                                         if (button.tag == 101) {
//                                             [self tanchuangshitu:@"网络错误,是否退出?"];
//                                          }
//                                     }
//                                 }
//                                 failure:^(NSError * err) {
//                                     
//                                     if (button.tag == 101) {
//                                         [self tanchuangshitu:@"发送失败,是否退出?"];
//                                     }else{
//                                     
//                                         [self.navigationController popViewControllerAnimated:YES];
//                                     }
//                                 }];
        }
    }
}

-(void)tanchuangshitu:(NSString *)tishiStr {
        UIAlertController *alertController=[UIAlertController alertControllerWithTitle:tishiStr message:nil preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *cancelButton=[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
        }];
        UIAlertAction *cretainButtn=[UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDestructive handler:^(UIAlertAction *action) {
            //点击按钮之后 回调的bolck
            [self.navigationController popViewControllerAnimated:YES];
        }];
        [alertController addAction:cancelButton];
        [alertController addAction:cretainButtn];
        [self presentViewController:alertController animated:YES completion:^{
            
        }];
}





-(void)pictureLongPress:(UILongPressGestureRecognizer *)longPress {
    
    NSArray * arrayLast = [[NSArray alloc] init];
    CGPoint point = [longPress locationInView:self.photoScrollView];
    NSString * strPoint =NSStringFromCGPoint(point);
    //NSLog(@"长按手指坐标----------------%@",strPoint);
    NSArray * array = [strPoint componentsSeparatedByString:@","];
    //NSLog(@"分割坐标-------%@",array[0]);
    //NSLog(@"分割坐标+++++++%@",array[1]);
    NSArray * arrayTwo = [array[0] componentsSeparatedByString:@"{"];
    //NSLog(@"最终需要坐标======%@",arrayTwo[1]);
    
    for (int i = 0; i < 10; i++) {
        if ([arrayTwo[1] intValue] > (110*i+10)) {
            arrayLast = [NSArray arrayWithObject:[NSString stringWithFormat:@"%d",i]];
        }
    }
    NSString * removeIndexStr = [NSString stringWithFormat:@"%@",[arrayLast lastObject]];
    //NSLog(@"removeIndexStr----------------%@",removeIndexStr);
    if ([removeIndexStr integerValue]+1 > self.photoFilePathArr.count) {
        NSLog(@"揍你");
    }
    else {
        UIAlertController * alertController = [UIAlertController alertControllerWithTitle:@"是否删除照片"
                                                                                  message:@""
                                                                           preferredStyle:UIAlertControllerStyleAlert];
        
        UIAlertAction * cancelButton = [UIAlertAction actionWithTitle:@"取消"
                                                                style:UIAlertActionStyleCancel
                                                              handler:^(UIAlertAction *action) {
                                                                  NSLog(@"取消");
                                                              }];
        UIAlertAction * creationBtn = [UIAlertAction actionWithTitle:@"删除"
                                                               style:UIAlertActionStyleDestructive
                                                             handler:^(UIAlertAction * _Nonnull action) {
                                                                 [_photoScrollView removeFromSuperview];
                                                                 _photoScrollView = nil;
                                                                 
                                                                 NSLog(@"removeIndexStr integerValue-----%ld",(long)[removeIndexStr integerValue]);
                                                                 [self.photoDescribeArr removeObjectAtIndex:[removeIndexStr integerValue]];
                                                                 [self.photoFilePathArr removeObjectAtIndex:[removeIndexStr integerValue]];
                                                                 [self.timeDateArr removeObjectAtIndex:[removeIndexStr integerValue]];
                                                                 if (_photoBtn.tag == 1000 + [removeIndexStr integerValue]) {
                                                                     [_photoBtn removeFromSuperview];
                                                                 }
                                                                 if (self.photoFilePathArr.count == 0) {
                                                                     self.backgroundImageView.image = [[UIImage alloc] init];
                                                                 }
                                                                 
                                                                 [self addPhoto];
                                                                 [self setPhotoInScrollView];
                                                             }];
        [alertController addAction:cancelButton];
        [alertController addAction:creationBtn];
        [self presentViewController:alertController animated:YES completion:^{
            
        }];
    }

}

/**添加照片*/
- (IBAction)addPhotoBtn:(UIButton *)sender {
    
    CTActionSheet *actionSheet = [CTActionSheet actionSheetWithTitle:nil cancelButtonTitle:@"取消" destructiveButtonTitle:@"拍照" otherButtonTitles:@"从相机相册选择" HandleBlock:^(int btnIndex) {
        if (btnIndex == 0) {
            if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera]) {
                UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypeCamera;
                UIImagePickerController *picker = [[UIImagePickerController alloc] init];//初始化
                picker.delegate = self;
                picker.allowsEditing = YES;//设置可编辑
                picker.sourceType = sourceType;
                [self presentViewController:picker animated:YES completion:^{
                }];//进入照相界面
            }
        }else if (btnIndex == 1){
            UIImagePickerController *pickerImage = [[UIImagePickerController alloc] init];
            if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
                pickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
                pickerImage.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:pickerImage.sourceType];
            }
            pickerImage.delegate = self;
            pickerImage.allowsEditing = YES;
            [self presentViewController:pickerImage animated:YES completion:^{
            }];
        }else{
        }
    }];
    UIWindow* window = [[[UIApplication sharedApplication] delegate] window];
    if ([window.subviews containsObject:self.view]) {
        [actionSheet showInView:self.view];
    } else {
        [actionSheet showInView:window];
    }
}

- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info{
    UIImage *originalImage =  (UIImage *) [info objectForKey:UIImagePickerControllerEditedImage];
    if(!originalImage){
        originalImage = (UIImage *) [info objectForKey:UIImagePickerControllerOriginalImage];
    }
    //拿到照片后
    [self setUpIconBtnWithImage:originalImage];
    self.backgroundImageView.image = originalImage;
    [picker dismissViewControllerAnimated:YES completion:nil];
}

//设置并上传图片
-(void)setUpIconBtnWithImage:(UIImage *)image{
    
    NSData *imageData = UIImageJPEGRepresentation(image, 1);
    if  (imageData.length>300000){
        UIImage *image = [UIImage imageWithData:imageData];
        imageData = UIImageJPEGRepresentation(image, 0.005);
    }
    [self getNetTime];
    NSLog(@"self.address-------%@",self.address);
    if (self.type == CompanyReportType) {
        if (self.address == nil) {
            _addressLabel.text = @"公司名称:   ";
        }
        else {
            _addressLabel.text = [NSString stringWithFormat:@"公司名称: %@",self.address];
        }
    }else {
        if (self.address == nil) {
            _addressLabel.text = @"工地名称:   ";
        }
        else {
            _addressLabel.text = [NSString stringWithFormat:@"工地名称: %@",self.address];
        }
    }
    //1。创建管理者对象
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer.acceptableContentTypes = [manager.responseSerializer.acceptableContentTypes setByAddingObjectsFromArray:@[@"application/json", @"text/json",@"text/html", @"text/javascript", @"text/plain"]];
    [manager POST:[NSString stringWithFormat:@"%@/Work/SendPic.ashx",API_HOST] parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:imageData name:@"img" fileName:@"name" mimeType:@"image/png"];
    } progress:^(NSProgress * _Nonnull uploadProgress) {
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        _dict = [[HXUpDataImageModel alloc]initWithDictionary:responseObject error:nil];
        if ([_dict.code isEqualToString:@"200"]) {
            [SVProgressHUD showSuccessWithStatus:@"图片上传成功"];
            //图片网址_dict.FilePath
            [self.photoFilePathArr addObject:_dict.FilePath];
            [self setPhotoInScrollView];
            [_describeTextView viewShow];
        }
        else{
            [SVProgressHUD showErrorWithStatus:@"图片上传失败"];
        }
        
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        [SVProgressHUD showErrorWithStatus:@"图上传失败"];
    }];
}


- (void)at_textViewDidEndEditingWithView:(UIView *) alertView withTextView:(UITextView *)at_textView {
    
    self.photoDescribeLabel.text = [NSString stringWithFormat:@"照片描述: %@",at_textView.text];
    [self.photoDescribeArr addObject:at_textView.text];
    [self loadCreatePicUpload];
    
}

- (void)at_textViewDidViewHidden:(UIView *)alertView withTextView:(UITextView *)at_textView{
    
    self.photoDescribeLabel.text = @"暂无描述";
    [self.photoDescribeArr addObject:@"暂无描述"];
    [self loadCreatePicUpload];
}

-(void)creatAlertController {

    _describeTextView = [[Imitation_AlertView_TextField alloc] initWithFatherViewFrameWidth:HXSCREEN_WIDTH withFrameHeight:HXSCREEN_HEIGHT];
    _describeTextView.title = @"照片描述";
    _describeTextView.delegate = self;
    [_describeTextView viewHidden];
//    [self.backGroundView addSubview:_describeTextView];
}

-(void)sendPicInformation {
    
    [self.sendMuArray removeAllObjects];
    for (int i = 0; i<self.photoFilePathArr.count; i++) {
        NSMutableDictionary * sendPicInformationDic = [[NSMutableDictionary alloc] init];
        [sendPicInformationDic setValue:self.con_TaskID forKey:@"Con_TaskID"];
        [sendPicInformationDic setValue:[UserDefaultHandler getUserDetault:kID] forKey:@"Con_UserID"];
        if (self.address == nil) {
            [sendPicInformationDic setValue:@"" forKey:@"Con_Add"];
            [sendPicInformationDic setValue:@"" forKey:@"Con_Title"];
        }
        else {
            [sendPicInformationDic setValue:self.Con_Title forKey:@"Con_Add"];
            [sendPicInformationDic setValue:self.address forKey:@"Con_Title"];
        }
        [sendPicInformationDic setValue:self.timeDateArr[i] forKey:@"Con_Date"];;
        [sendPicInformationDic setValue:self.photoDescribeArr[i] forKey:@"Con_Content"];
        [sendPicInformationDic setValue:self.photoFilePathArr[i] forKey:@"Con_PicUrl"];
        [self.judgeArray addObject:self.photoFilePathArr[i]];
        if (self.type == SiteReportType) {
            [sendPicInformationDic setValue:@"3" forKey:@"Con_PicType"];
        }
        else {
            [sendPicInformationDic setValue:@"5" forKey:@"Con_PicType"];
        }
        [sendPicInformationDic setValue:@"true" forKey:@"Con_IsSend"];
        [sendPicInformationDic setValue:@"" forKey:@"Con_PunishContent"];
        [sendPicInformationDic setValue:@"" forKey:@"Con_PunishID"];
        [sendPicInformationDic setValue:@"false" forKey:@"Con_IsPunish"];
        [sendPicInformationDic setObject:self.Con_SendDate.length <= 0 ? @"":self.Con_SendDate forKey:@"Con_SendDate"];
        
        [self.sendMuArray addObject:sendPicInformationDic];
        NSLog(@"sendPicInformationDic-------------------%@",self.sendMuArray);
    }
}


/**
 * 上传照片的方法
 */
-(void)loadCreatePicUpload{

    [self sendPicInformation];
    NSLog(@"self.sendMuArray------%@",self.timeDateArr);
    NSDictionary *dictModelOne = @{@"dict":self.sendMuArray};
    NSData * data = [NSJSONSerialization dataWithJSONObject:dictModelOne options:NSJSONWritingPrettyPrinted error:nil];
    NSString * string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"json------------------------%@",string);
    [NetworkAPI createAddWithDic:dictModelOne
                         success:^(HXSendInformationModel *result) {
                             
                             if (self.isCon_SendBtn) {
                                 
                                 if ([result.code isEqualToString:@"200"]) {
                                     //[SVProgressHUD showSuccessWithStatus:@"发送成功"];
                                     [self tanchuangshitu:@"提交成功,是否退出?"];
                                 }
                                 else {
                                     [self tanchuangshitu:@"网络错误,是否退出?"];
                                     
                                 }
                             }
                         } failure:^(NSError * err) {
                             
                             if (self.isCon_SendBtn) {
                                 [self tanchuangshitu:@"发送失败,是否退出?"];
                             }
                         }];


}



/**
 设置scrollView
 */
-(void)setPhotoInScrollView {
    for (int i = 0; i<self.photoFilePathArr.count; i++) {
        _photoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _photoBtn.tag = 1001 + i;
        _photoBtn.frame = CGRectMake(10 + 110 * i, 0, 100, 100);
        [_photoBtn sd_setImageWithURL:[NSURL URLWithString:self.photoFilePathArr[i]] forState:UIControlStateNormal placeholderImage:nil
                            completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                                self.backgroundImageView.image = _photoBtn.imageView.image;
                                if (error) {
                                }
                            }];
        [_photoBtn addTarget:self action:@selector(photoBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        //_photoBtn.backgroundColor = [UIColor greenColor];
        [_photoScrollView addSubview:_photoBtn];
    }

    if (self.photoFilePathArr.count == 0) {
        self.photoDescribeLabel.text = [NSString stringWithFormat:@"照片描述: "];
    }else {
        if (self.photoDescribeArr.count == 0) {
            self.photoDescribeLabel.text = @"照片描述:   ";
        }
        else {
            self.photoDescribeLabel.text = [NSString stringWithFormat:@"照片描述: %@",[self.photoDescribeArr lastObject]];
        }
    }
    _photoScrollView.contentSize = CGSizeMake(110 * (self.photoFilePathArr.count + 1), 100);
}
-(void)photoBtnClick:(UIButton *)btn {
    
    for (int i = 0; i<self.photoFilePathArr.count; i++) {
        if (btn.tag  == 1001 + i) {
            self.photoDescribeLabel.text = [NSString stringWithFormat:@"照片描述: %@",self.photoDescribeArr[i]];
            self.sendTimeLabel.text =[NSString stringWithFormat:@"发送时间: %@",self.timeDateArr[i]];
            self.backgroundImageView.image = btn.imageView.image;
        }
    }
}

/**
 展示scrollView
 */
-(void)getPhotoInScrollView {
    
    if (self.Resultlist.count == 0) {
        return;
    }
    for (int i = 0; i<self.Resultlist.count; i++) {
        _photoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _photoBtn.tag = 1001 + i;
        _photoBtn.frame = CGRectMake(10 + 110 * i, 0, 100, 100);
        [_photoBtn sd_setImageWithURL:[NSURL URLWithString:self.photoFilePathArr[i]] forState:UIControlStateNormal placeholderImage:nil
                       completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
                           self.backgroundImageView.image = _photoBtn.imageView.image;
                           if (error) {
                           }
                       }];
        [_photoBtn addTarget:self action:@selector(photoButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        //_photoBtn.backgroundColor = [UIColor greenColor];
        [_photoScrollView addSubview:_photoBtn];
    }
    if (self.type == CompanyReportType) {
        if (self.address == nil) {
            _addressLabel.text = @"公司名称:   ";
        }
        else {
            _addressLabel.text = [NSString stringWithFormat:@"公司名称: %@",self.address];
        }
    }else {
        if (self.address == nil) {
            _addressLabel.text = @"工地名称:   ";
        }
        else {
            _addressLabel.text = [NSString stringWithFormat:@"工地名称: %@",self.address];
        }
    }
    self.photoDescribeLabel.text = [NSString stringWithFormat:@"照片描述: %@",[self.photoDescribeArr lastObject]];
    self.sendTimeLabel.text =[NSString stringWithFormat:@"发送时间: %@",self.timeDateArr[0]];
    _photoScrollView.contentSize = CGSizeMake(110 * (self.photoFilePathArr.count + 1), 100);
}

-(void)photoButtonClick:(UIButton *)btn {
    
    for (int i = 0; i<self.Resultlist.count; i++) {
        
        if (btn.tag  == 1001 + i) {
            self.photoDescribeLabel.text = [NSString stringWithFormat:@"照片描述: %@",self.photoDescribeArr[i]];
            self.sendTimeLabel.text =[NSString stringWithFormat:@"发送时间: %@",self.timeDateArr[i]];
            self.backgroundImageView.image = btn.imageView.image;
        }
    }
}

/**
 获取网络时间
 */
-(void)getNetTime {
    [NetworkAPI getServerDateSuccess:^(HXServerDateModel *result) {
        if ([result.code isEqualToString:@"200"]) {
            self.sendTimeLabel.text =  [NSString stringWithFormat:@"发送时间: %@",result.ServerDate];
            self.sendTimeStr = result.ServerDate;
            [self.timeDateArr addObject:result.ServerDate];
        }
    } failure:^(NSError *err) {
        [SVProgressHUD showErrorWithStatus:@"获取网络时间失败,请检查网络"];
    }];
}

//判断添加按钮是否可用
-(void)judgeIsChange {
    
    if (self.Con_SendDate.length <= 0) {
        
        return;
    }
    //将列表时间转化为Date数据
    NSDateFormatter * dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    NSDate *netTimeDate = [dateFormatter dateFromString:self.Con_SendDate];
    //将列表时间转化为秒数 将秒转化为毫秒 *1000
    NSTimeInterval NowSecond=[netTimeDate timeIntervalSince1970] * 1000;
    NSString *timeString = [NSString stringWithFormat:@"%.0f", NowSecond];
    /**
     *能修改的时间将现在时间转换成工作日
     */
//    NSCalendar * myCalendar = [NSCalendar currentCalendar];
//    myCalendar.timeZone = [NSTimeZone systemTimeZone];
//    NSInteger week = [[myCalendar components:NSCalendarUnitWeekday fromDate:netTimeDate] weekday];
    
    //当前时间秒数
    //将列表时间转化为Date数据
    NSDate* nowDate = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval nowTimeSecond=[nowDate timeIntervalSince1970] * 1000;
    NSString * nowTimeString = [NSString stringWithFormat:@"%.0f",nowTimeSecond];
    NSInteger oneDay = 24*60*60*1;  //1天的长度 
    
    //周日 周一 周二 周三 周四
    NSInteger dis = 1; //前后的天数
    if (([timeString integerValue] + (dis * oneDay * 1000)) >= [nowTimeString integerValue]) {
            self.sendBtn.hidden = NO;
    }
    else {
        self.sendBtn.hidden = YES;
        [_photoScrollView removeGestureRecognizer:_longPress];
        self.addPhotoBtn.backgroundColor = [UIColor clearColor];
        self.addPhotoBtn.enabled = NO;
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

-(NSMutableArray *)phothMuArray {
    if (_phothMuArray == nil) {
        _phothMuArray = [[NSMutableArray alloc] init];
    }
    return _phothMuArray;
}
-(NSMutableArray *)photoFilePathArr {
    if (_photoFilePathArr == nil) {
        _photoFilePathArr = [[NSMutableArray alloc] init];
    }
    return _photoFilePathArr;
}
-(NSMutableArray *)photoDescribeArr {
    if (_photoDescribeArr == nil) {
        _photoDescribeArr = [[NSMutableArray alloc] init];
    }
    return _photoDescribeArr;
}
-(NSMutableArray *)sendMuArray {
    if (_sendMuArray == nil) {
        _sendMuArray = [[NSMutableArray alloc] init];
    }
    return _sendMuArray;
}
-(NSMutableArray *)getDataArr {
    if (_getDataArr == nil) {
        _getDataArr = [[NSMutableArray alloc] init];
    }
    return _getDataArr;
}
-(NSMutableArray *)timeDateArr {
    if (_timeDateArr == nil) {
        _timeDateArr = [[NSMutableArray alloc] init];
    }
    return _timeDateArr;
}
-(NSMutableArray *)judgeArray {
    if (_judgeArray == nil) {
        _judgeArray = [[NSMutableArray alloc] init];
    }
    return _judgeArray;
}



@end
